//
//  LZYTabBarViewController.m
//  LZYTabbarController
//
//  Created by 刘宇 on 2018/5/3.
//  Copyright © 2018年 瀚宇悟道. All rights reserved.
//

#import "LZYTabBarViewController.h"


@interface LZYTabBarViewController ()<LZYTabBarDelegate>

@end

@implementation LZYTabBarViewController

- (instancetype)initWithTabBarControllers:(NSArray *)controllers NorImageArr:(NSArray *)norImageArr SelImageArr:(NSArray *)selImageArr TitleArr:(NSArray *)titleArr Config:(LZYTabBarConfig *)config{
    self.viewControllers = controllers;
    self.lzyTabBar = [[LZYTabBar alloc] initWithFrame:self.tabBar.frame norImageArr:norImageArr SelImageArr:selImageArr TitleArr:titleArr config:config];
    
    self.lzyTabBar.lzyTabBarDelegate = self;
    
    
    [self setValue:self.lzyTabBar forKeyPath:@"tabBar"];
    
    
    [LZYTabBarConfig shareConfig].tabBarController = self;
    
    //KVO
    [self addObserver:self forKeyPath:@"selectedIndex" options:NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew context:nil];
    
    return self;
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
    NSInteger selectedIndex = [change[@"new"] integerValue];
    self.lzyTabBar.selectedIndex = selectedIndex;
}

- (void)tabBar:(LZYTabBar *)tabBar didSelectIndex:(NSInteger)selectIndex {
    self.selectedIndex = selectIndex;
}

- (void)dealloc {
    LZYLog(@"被销毁了");
    [self removeObserver:self forKeyPath:@"selectedIndex"];
}

@end
